import Vue from 'vue'
import Router from 'vue-router'
// 默认取views下的vue文件
const _import = file => () => import('@/views/' + file + '.vue')
Vue.use(Router)

// 路由
const routerMap = [
  {
    path: '/',
    component: _import('layout/layout'),
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: _import('home/index'),
        meta: {
          activedIndex: 0,
          title: '首页',
          keepAlive: true
        }
      },
      {
        path: '/my',
        name: 'My',
        component: _import('my/index'),
        meta: {
          activedIndex: 1,
          title: '个人中心',
          keepAlive: true
        }
      }
    ]
  }
]

export default new Router({
  mode: 'history',
  base: process.env.ROUTER_BASE,
  // 如果开启了keepAlive: true 的配置 需要处理滚动位置
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: to.meta.savedPosition || 0 }
    }
  },
  routes: routerMap
})
